/*
会飞的字
*/


var { Plot } = require("../Plot.js");
var { Point } = require("../Point")
/**
 * @type {HTMLCanvasElement}
 */
var canvasDom = document.querySelector("#myCanvas");
var plot = new Plot(canvasDom)
var context = canvasDom.getContext("2d");
canvasDom.width = document.body.clientWidth;;
canvasDom.height = document.body.clientHeight;

var querystring = require("querystring");
var params = querystring.parse(location.search.slice(1));

context.font = `normal ${canvasDom.height * 0.3}px "楷体"`;
params.word = params.word || '人'
context.strokeText(params.word , canvasDom.width / 2 - canvasDom.height * 0.3 / 2 * params.word.length, canvasDom.height / 2 );

setTimeout(function () {
	/**
	 * @type {{x:number,px:number,y:number,py:number,s:number,a:number}[]}
	 */
	// @ts-ignore
	var list = plot.pickColor([0, 0, 0, 255], 50);
	var center = plot.center;
	var lenList2Center = []
	for (let index = 0, len = list.length; index < len; index++) {
		// const element = array[index];
		list[index].px = list[index].x;
		list[index].py = list[index].y;

		// @ts-ignore
		lenList2Center.push(Point.len(list[index], center))
	}
	var maxLeng = Math.max(...lenList2Center);
	var circleR = maxLeng * 1.1;
	// debugger

	for (let index = 0, len = list.length; index < len; index++) {
		var angle = Math.atan((list[index].py - center.y) / (list[index].px - center.x));
		if (list[index].x - center.x < 0) {
			angle += Math.PI;
		}
		// @ts-ignore
		// lenList2Center.push(Point.len(list[index], center))
		list[index].s = Math.asin(lenList2Center[index] / circleR - 0.5)
		list[index].a = Math.asin(angle / Math.PI / 2 - 0.5)
	}

	var tick = 0;
	list.forEach(v => {
		v.px = v.x;
		v.py = v.y;
	})
	plot.drawList(list);

	function draw() {
		tick++;
		var center = plot.center;
		for (var i = 0; i < list.length; i++) {
			var ag = (Math.sin(list[i].a + tick * 0.005) + 0.5) * Math.PI * 2;
			var ll = (Math.sin(list[i].s + tick * 0.01) + 0.5) * circleR
			list[i].x = ll * Math.cos(ag) + center.x;
			list[i].y = ll * Math.sin(ag) + center.y;

			// angle /= Math.PI / 360; 
		}
		plot.drawList(list);

	}
	setInterval(draw, 10);
	globalThis.list = list;

}, 1000)



// var list = getPoints()
// var context = plot.context;
// context.font = 'normal 28pt "楷体"';
// context.fillStyle = "#000000";
// context.clearRect(0,0,canvas.width,canvas.height);  

